home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: in1.uu.net!bcstec!schultz
- From: schultz@bcstec.ca.boeing.com (Robert A. Schultz)
- Subject: Re: How to remove a substring from a string
- Message-ID: <DKxL38.8K1@bcstec.ca.boeing.com>
- Organization: The Boeing Company
- X-Newsreader: TIN [version 1.2 PL2]
- References: <4cu5br$ejb@erinews.ericsson.se>
- Date: Tue, 9 Jan 1996 20:33:07 GMT
-
- Inger Dufva Z/OF (eraida@lmera.ericsson.se) wrote:
- : Hi,
-
- : I would like to remove a sub string from a string in C. An example:
-
- : string: howdoyoudo
- : sub_string: you
-
- : After this function (called remove_substring here) has been called:
-
- : new_string = remove_substring(string, sub_string);
-
- : new_string should have the value: howdodo
-
- : Can anyone please help me with this function? Thanks in advance!
- : Cheers,
-
- For shame! There must be more to your homework problem text than this. I.e.
- Destructive or non-destructive? Just the first occurance of sub_string or
- all of them (Begs for a recursive solution.)? Are there any "forbidden" calls
- to the ANSI standard functions? If you want an "A" you are just going to have
- to be more specific.
-
- Never-the-less both
-
- char *remove_substring(char *string, char *sub_string)
- {
- return "howdodo";
- }
-
- and
-
- char *remove_substring(char *string, char *sub_string)
- {
- char *cp;
- if ((cp=strstr(string, sub_string)) != NULL)
- memmove(cp, cp+strlen(sub_string), strlen(cp)-strlen(sub_string)+1);
- return string;
- }
-
- satisfy the letter of your question if not the spirit. :)
-
- --
- =''' Rob Schultz
- c-oo TSAP Programmer Guy
- \ Boeing Tech Services Systems
- - veni vidi recodei (206) 544-0793 MS 2H-31
-